/**
 * FileName: DeviceController
 * Author: Wb
 * Date: 2025/9/24 9:48
 * Description:
 * History:
 */
package org.test.device.controller;


import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.test.device.constant.DeviceStatusEnum;
import org.test.device.params.QueryDeviceParam;
import org.test.device.params.SaveDeviceParam;
import org.test.device.params.UpdateDeviceParam;
import org.test.device.service.DeviceService;
import org.test.device.utils.ResultData;
import org.test.device.vo.device.DeviceReceiveInfo;
import org.test.device.vo.device.DeviceVoList;

import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈设备Controller〉
 *
 * @author Wb
 * @create 2025/9/24
 * @since 1.0.0
 */
@Slf4j
@Tag(name = "设备管理接口", description = "设备注册、状态查询、数据上报相关接口")
@RestController
@RequestMapping("device")
public class DeviceController {

    @Autowired
    @Qualifier("deviceServiceImpl")
    DeviceService deviceService;

    /**
     * @Author Wb
     * @Description 查询列表数据-分页-支持条件查询
     * @Date 15:37 2025/9/24
     * @Param DeviceParam
     * @Return org.test.device.utils.ResultData
     */
    @GetMapping("list")
    @Operation(summary = "查询列表数据-分页-支持条件查询", description = "传入查询参数，返回设备列表数据")
    public ResultData getDeviceList(@ParameterObject QueryDeviceParam param){
        PageHelper.startPage(param.getCurrentPage(), param.getPageSize());
        List<DeviceVoList> deviceVoListVoList=deviceService.getDeviceList(param);
        PageInfo<DeviceVoList> pageInfo = new PageInfo<>(deviceVoListVoList);
        log.info("查询结果：{}，总数：{}，总页数：{}，当前页：{}", deviceVoListVoList, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum());
        return ResultData.pageResponse(deviceVoListVoList, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum());
    }

    /**
    * @Author Mr.Wang
    * @Description 新增设备信息
    * @Date 14:15 2025/9/28
    * @param
    **/
    @PostMapping
    public ResultData saveDevice(@RequestBody SaveDeviceParam param){
        log.info("新增设备数据:{}", JSON.toJSONString(param));
        deviceService.saveDevice(param);
        return new ResultData("添加成功！");
    }

    /**
    * @Author Mr.Wang
    * @Description 更新设备信息
    * @Date 14:15 2025/9/28
    * @param
    **/
    @PutMapping("{code}")
    public ResultData updateDevice(@PathVariable String code,
                                   @RequestBody UpdateDeviceParam param){
        log.info("更新设备code:{},更新信息:{}",code,param);
        return deviceService.updateDevice(code,param);
    }

    /**
    * @Author Mr.Wang
    * @Description 删除设备信息
    * @Date 14:15 2025/9/28
    * @param
    **/
    @DeleteMapping("ids")
    public ResultData deletedDevice(){

        return new ResultData("");
    }

    /**
    * @Author Mr.Wang
    * @Description 认证设备
    * @Date 14:17 2025/9/28
    * @param
    **/
    @PutMapping("{code}/certification")
    public ResultData certificationDevice(@PathVariable String code,
                                          @RequestParam String clientId,
                                          @RequestParam String authPassword){
        log.info("对设备code:{}-进行认证！",code);
        return deviceService.certificationDevice(code,clientId,authPassword);
    }

    /**
    * @Author Mr.Wang
    * @Description 启用设备
    * @Date 10:38 2025/9/29
    * @param
    **/
    @PutMapping("{code}/updateStatus")
    public ResultData updateDeviceStatus(@PathVariable String code,
                                         @RequestParam Integer status){
        log.info("修改设备启用停止code:{},状态:{}",code, DeviceStatusEnum.getByCode(status));
        return deviceService.updateDeviceStatus(code,status);
    }

    /**
    * @Author Mr.Wang
    * @Description 发送信息到设备
    * @Date 16:01 2025/9/29
    * @param topic 设备
    * @param info 接收到到信息
    **/
    @PostMapping("sendInfo")
    public ResultData deviceSendInfo(@RequestParam String topic, @RequestBody DeviceReceiveInfo info){
        return deviceService.deviceSendInfo(topic,info);
    }

    /**
    * @Author Mr.Wang
    * @Description 模拟接收外部设备到信息
    * @Date 17:06 2025/9/29
    * @param 
    **/
    @PostMapping("receiveFromExternal")
    public ResultData receiveFromExternal(
            @RequestParam String topic,
            @RequestBody DeviceReceiveInfo info) {
        return deviceService.receiveExternalMessage(topic, info);
    }

    /**
    * @Author Mr.Wang
    * @Description 接收设备信息
    * @Date 16:27 2025/9/29
    * @param
    **/
    @GetMapping("getReceivedMessages")
    public ResultData getDeviceReceivedMessages(@RequestParam String topic) {
        return deviceService.getDeviceMessagesByTopic(topic);
    }
}